﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management.Automation;

namespace AppFabricAdministrator
{
    public partial class Permission : UserControl
    {
        private string addUserCmdFormat = "Grant-CacheAllowedClientAccount -Account \"{0}\"";
        private string getAllowedClientsCmdFormat = "Get-CacheAllowedClientAccounts";
        private IEnumerable<string> allowedAccounts;
        public Permission()
        {
            InitializeComponent();
            GetGrantedUsers();
        }

        private void GetGrantedUsers()
        {
            allowedAccounts = Enumerable.Empty<string>();
            bool isSuccess;
            ScriptHelper.RunScript(getAllowedClientsCmdFormat, "", this.GetAllowedUsers, out isSuccess);
            SetAllowedUser();
        }
        List<SinglePermittedUser> spusrs = new List<SinglePermittedUser>();
        private void SetAllowedUser()
        {
            spusrs = new List<SinglePermittedUser>();
            for (int i = 0; i < this.allowedAccounts.Count(); i++)
            {
                SinglePermittedUser u = new SinglePermittedUser(this.allowedAccounts.ElementAt(i), this.Remove);
                spusrs.Add(u);
            }
            AddUserControls();
        }
        private void AddUserControls()
        {
            int s = 32;
            this.splitContainer2.Panel2.Controls.Clear();
            for (int i = 0; i < this.spusrs.Count; i++)
            {
                this.spusrs[i].Location = new Point(0, i * s);
            }
            this.splitContainer2.Panel2.Controls.AddRange(spusrs.ToArray());
        }
        public void Remove(SinglePermittedUser u)
        {
            this.spusrs.Remove(u);
            AddUserControls();
        }

        public string GetAllowedUsers(IEnumerable<PSObject> psobjects)
        {
            List<string> allUsers = new List<string>();
            foreach (var pso in psobjects)
            {
                allUsers.Add(pso.BaseObject.ToString());
            }
            this.allowedAccounts = allUsers;
            return null;
        }
        private void btnAddUsr_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.txtAddUser.Text))
            {
                string[] users = this.txtAddUser.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var user in users)
                {
                    if (!string.IsNullOrWhiteSpace(user))
                    {
                        string cmd = string.Format(addUserCmdFormat, user);
                        bool isSuccess = false;
                        ScriptHelper.RunScript(cmd, "Successfully added user", null, out isSuccess);
                        if (isSuccess)
                            GetGrantedUsers();
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddUserForm form = new AddUserForm();
            string user;
            if (form.ShowAddUserDialog(out user).Equals(DialogResult.OK))
            {
                if (!string.IsNullOrWhiteSpace(user))
                {
                    this.txtAddUser.Text += Environment.NewLine + user;
                    this.txtAddUser.Text = this.txtAddUser.Text.Trim(new char[] { '\r', '\n', '\t' });
                }
            }
        }


    }
}
